import { 
    shop_shou,
    shop_quxia
   } from '../../utils/api'
import { Toast } from "vant";
export default{
    namespaced:true,//开启模块命名
    state:{
        shoplist:[],
    },
    mutations:{
        fav(state,params){//收藏店铺
            if(params){
              state.shoplist.push(params)
            }
          },
          unfav(state,params){//取消收藏店铺
            //查找店铺
            var index =state.shoplist.findIndex((item)=>{
              return item.mtWmPoiId==params.mtWmPoiId
            })
            if(index!=-1){
              state.shoplist.splice(index,1)
            }
          }
    },
    actions:{
        request_fav(store,shop){//发请求, 收藏店铺
            var userinfo = localStorage.getItem('userinfo') ? JSON.parse( localStorage.getItem('userinfo') ) : {};
            
          
              shop_shou( { userid:userinfo.id,shopid:shop.mtWmPoiId } ).then((res)=>{
               if(res.data.code==200){
                
                 store.commit('fav',shop)
                 //弹出提示框（这种成功 /失败的提示需要放在异步回调中）
                 Toast({message:res.data.msg,type:'success'});
               }
              })
            },
        request_unfav(store,shop){//发请求, 收藏店铺
              var userinfo = localStorage.getItem('userinfo') ? JSON.parse( localStorage.getItem('userinfo') ) : {};
              
            
                shop_quxia( { userid:userinfo.id,shopid:shop.mtWmPoiId } ).then((res)=>{
                 if(res.data.code==200){
                   store.commit('unfav',shop);
                   //弹出提示框
                   Toast({message:res.data.msg,type:'success'});
                 }
                })
              }
    }
}